Ext.define('Triends.view.Login', {
	extend: 'Ext.Panel',
	requires: ['Triends.view.AgreementConfirm'],
	xtype: 'login',
	id: 'login',
	alias: 'login',
	config: {
		fullscreen: true,
		style: 'background:white',
    	width: '100%',
    	showAnimation: 'slideIn',
    	height: '100%',
    	layout: 'vbox',
    	listeners: {
	    	deactivate: function() {
	    		this.destroy();
	    	}
	    },
		items: [
			{
					xtype: 'panel',
					style: {'left':'50%', 'margin-left':'-37.5%', 'margin-top':'10%'},
					height: '75%',
					width: '75%',
					html: "<img src='resource/images/triendsaccount1.png'>"
			},
			{
			xtype: 'panel',
			layout: 'vbox',
			style: {'margin-top':'15%'},
			items:[
				{
					xtype: 'panel',
					xtype: 'panel',
					style: {'left':'50%', 'margin-left':'-130px'},
					width: '278px',
					html: '<font size="2"><b>클릭 한 번으로 간편하게 로그인하세요</b></font>'
				},
				{
	        	xtype: 'button',
				style: {'left':'50%', 'margin-left':'-139px', 'margin-top':'3px'},
	            name: 'Login',
	           	width: '278px',
		        height: '44px',
	            cls: 'FBlogin',
	            pressedCls: 'pressedlogin',
				handler: function(){
            	FB.login(function(response) {
            		Ext.Viewport.setMasked({
								xtype: 'loadmask',
								message: '연결 중입니다.',
								indicator: true
							});
					FB.api('/me', function(response) {
            				Ext.data.JsonP.request({
            				url: 'http://itscreater.cafe24.com/Triends/Login.jsp',
            				params: {
            					id:response.id,
            					name:response.name,
            					email:response.email, 
            					age:response.age,
            					gender:response.gender
            				},
            				callbackKey: 'callback',
            				success: function(result) {
            					if(result.message == "Good") {
            						Ext.Viewport.setMasked(false);
            						Ext.Viewport.setMasked({
										xtype: 'loadmask',
										message: '로그인 중입니다.',
										indicator: true
									});
            						
            						if (Ext.typeOf(Ext.getCmp('main')) == 'object') {
            							Ext.Viewport.setActiveItem(Ext.getCmp('main'));
            						} else {
            							var main = Ext.create('mainpage');
            							Ext.Viewport.setActiveItem(main);
            						}

            						Triends.app.getHistory().add(Ext.create('Ext.app.Action', {url: 'mainpage'}));

									var UserStore = Ext.getStore('UserStore');
							    	UserStore.getProxy().setExtraParams({id: response.id});
            						UserStore.load({
				        				callback: function(records, operation, success) {
				        					MainDataLoad();
				        				}
            						});
            					} else if (result.message == "Bad") {
            						Ext.Viewport.setMasked(false);
		           					var agrmtconfirm = Ext.create('Triends.view.AgreementConfirm');
		           		
		           					Ext.data.JsonP.request({
		           						url: 'http://itscreater.cafe24.com/Triends/UserAgreement.jsp',
		           						callbackKey: 'callback',
		           						success: function(result){
		           							var agreementpanel = Ext.getCmp('agreementpanel');
		           							agreementpanel.setHtml('<font size="2">'+result.Agree+'</font>');
		           							agrmtconfirm.setData(response); // facebook계정정보 방출
		           							Ext.Viewport.add(agrmtconfirm);
		           						}
		           					});
            					}
            				},
            				failure: function(result) {
            					Ext.Viewport.setMasked(false);
            					navigator.notification.alert(
            						'서버 실행 오류',
            						function(){},
            						'알림',
            						'확인'
            					)
            				}
	            			});	
						});	
	            	}, {scope: 'user_birthday'});
	            }
			},
			{
					xtype: 'panel',
					xtype: 'panel',
					style: {'left':'50%', 'margin-left':'-130px', 'margin-top':'5%'},
					width: '278px',
					html: '<font size="1" color="grey">본 서비스는 <br>Facebook 계정을 통해서만 로그인이 가능합니다.</font>'
			}]
		}]
	}
});

